<?php

namespace app\controllers;

use Behat\Gherkin\Exception\Exception;
use Yii;
use yii\base\UserException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\HttpException;

class SiteController extends BaseController
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {

        return [
            'access' => [
                'class' => AccessControl::className(),
                // 'only' => ['logout', 'index'],
                'rules' => [
                    [
                        'actions' => [
                            'index',
                            'error',
                            'backend-error',
                            'captcha',
                        ],
                        'allow' => true,
                        'roles' => ['?']
                    ],
                    [
                        'actions' => [
                            'index',
                            'error',
                            'backend-error',
                            'captcha',
                        ],
                        'allow' => true,
                        'roles' => ['@']
                    ]
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [

                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {

        return [
            'captcha' => [
                'class' => 'app\service\CaptchaService',
                // 'fixedVerifyCode' => substr(md5(time()), 11, 4),
                'backColor' => 0xFFFFFF,//背景颜色
                'maxLength' => 5, //最大显示个数
                'minLength' => 5,//最少显示个数
                'padding' => 0,//间距
                'height' => 40,//高度
                'width' => 105,  //宽度
                'foreColor' => 0x000000,     //字体颜色
                'offset' => 4,        //设置字符偏移量 有效果
                // 'fontFile' =>'' // 规定字体文件路径 example：dirname(Yii::$app->basePath) . '/source/font/elephant.ttf'
                //'controller'=>'login',        //拥有这个动作的controller
            ],
        ];
    }

    public function actionError()
    {

        $url = $this->request->url;
        if (strstr($url, '/admin/')) {
            return $this->actionBackendError();
        } else {
            return $this->actionFrontError();
        }
    }

    /**
     * 前台 错误页面
     * @return string|\yii\console\Response|\yii\web\Response
     */
    public function actionFrontError()
    {

        // 错误信息
        $exception = Yii::$app->getErrorHandler()->exception;

        // 错误类型
        if ($exception instanceof HttpException) {
            $code = $exception->statusCode;
        } else {
            $code = $exception->getCode();
        }

        // 错误类型
        if ($exception instanceof Exception) {
            $name = $exception->getName();
        } else {
            $name = Yii::t('yii', 'Error');
        }

        if (empty($code)) $code = 500;

        // 错误类型
        if ($exception instanceof UserException) {
            $message = $exception->getMessage();
        } else {
            $message = Yii::t('yii', 'An internal server error occurred.');
        }

        if (Yii::$app->getRequest()->getIsAjax()) {

            return $this->jsonFail($message, $code, [
                'errorMessage' => $message,
                'errorCode' => $code
            ]);
        } else {

            // 统一走前台即可
            return $this->showError($message, $code, [
                'moduleName' => 'frontHtml'
            ]);
        }
    }

    /**
     * 后台 错误页面
     * @return string|\yii\console\Response|\yii\web\Response
     */
    public function actionBackendError()
    {

        $previousPage = Yii::$app->getModules();
        $exception = Yii::$app->getErrorHandler()->exception;

        ### 响应对象
        $res = \Yii::$app->getResponse(); //响应头部类文件

        if ($exception instanceof HttpException) {
            $httpCode = $exception->statusCode;
        } else {
            $httpCode = $exception->getCode();
        }
        $httpCode = empty($httpCode) ? 400 : $httpCode;

        if ($exception instanceof Exception) {
            $name = $exception->getName();
        } else {
            $name = Yii::t('yii', 'Error');
        }

        if ($exception instanceof UserException) {
            $message = $exception->getMessage();
        } else {
            $message = Yii::t('yii', 'An internal server error occurred.');
        }

        if (Yii::$app->getRequest()->getIsAjax()) {

            return $this->jsonFail($message, $httpCode, [
                'errorMessage' => $message,
                'errorCode' => $httpCode
            ]);
        } else {

            // 统一走前台即可
            return $this->showError($message, $httpCode, [
                'moduleName' => 'backendHtml'
            ]);
        }
    }
}
